home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 124 / cd-rom 124.iso / edu / tuxmath / tuxmathscrabble / asymptopia / Localizer.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-11-16  |  8.2 KB  |  300 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. '''
  5. /***************************************************************************
  6.  
  7. \tAuthor \t\t\t:Charles B. Cosse 
  8. \t
  9. \tEmail\t\t\t:ccosse@asymptopia.com
  10. \t\t\t\t\t
  11. \t\t\t\t\t
  12. \tCopyright\t\t:(C) 2002,2003 Asymptopia Software.
  13. \t
  14.  ***************************************************************************/
  15. /***************************************************************************
  16.                           Localizer.py
  17.  
  18.  
  19.  ***************************************************************************/
  20.  
  21. /***************************************************************************
  22.  *                                                                         *
  23.  *   This program is free software; you can redistribute it and/or modify  *
  24.  *   it under the terms of the GNU General Public License as published by  *
  25.  *   the Free Software Foundation; either version 2 of the License, or     *
  26.  *   (at your option) any later version. (Please note that if you use this *
  27.  *   code you must give credit by including the Author and Copyright       *
  28.  *   info at the top of this file).                                        *
  29.  ***************************************************************************/
  30.  
  31. '''
  32. import pygame
  33. import os
  34. from pygame.locals import *
  35. from random import random
  36.  
  37. class Localizer:
  38.     '''Localizer has game model.
  39. \t'''
  40.     
  41.     def __init__(self, board, game):
  42.         self.board = board
  43.         self.game = game
  44.         self.board_map = None
  45.         self.M = self.board.M
  46.         self.N = self.board.N
  47.  
  48.     
  49.     def update_board_map(self):
  50.         self.board_map = self.board.get_map()
  51.  
  52.     
  53.     def localize(self, submission):
  54.         rand = int(random() * 2)
  55.         if rand == 0:
  56.             rlist = self.try_row(submission)
  57.             if rlist:
  58.                 return rlist
  59.             
  60.             rlist = self.try_col(submission)
  61.             if rlist:
  62.                 return rlist
  63.             
  64.         else:
  65.             rlist = self.try_col(submission)
  66.             if rlist:
  67.                 return rlist
  68.             
  69.             rlist = self.try_row(submission)
  70.             if rlist:
  71.                 return rlist
  72.             
  73.         return None
  74.  
  75.     
  76.     def try_row(self, submission):
  77.         board_map = self.board_map
  78.         M = self.M
  79.         N = self.N
  80.         if len(submission) == 0:
  81.             return None
  82.         
  83.         if len(self.board.get_listofheads()) == 0:
  84.             return None
  85.         
  86.         slim = len(submission)
  87.         nlim = (N - len(submission)) + 1
  88.         for m in range(M):
  89.             for n in range(nlim):
  90.                 ok = 1
  91.                 for sidx in range(slim):
  92.                     if submission[sidx][:3] == 'WC:':
  93.                         if board_map[m][n + sidx] == submission[sidx][3:]:
  94.                             dummy = 0
  95.                         else:
  96.                             ok = 0
  97.                     elif board_map[m][n + sidx] == '':
  98.                         dummy = 0
  99.                     else:
  100.                         ok = 0
  101.                 
  102.                 if ok == 1:
  103.                     rlist = []
  104.                     for idx in range(slim):
  105.                         tripple = [
  106.                             submission[idx],
  107.                             m,
  108.                             n + idx]
  109.                         rlist.append(tripple)
  110.                     
  111.                     rval = self.check_neighborhood(rlist)
  112.                     tuxscore = 0
  113.                     multiplier = 1
  114.                     for nn in range(n, n + len(rlist)):
  115.                         spot2check = self.board.get_spotMN(m, nn)
  116.                         if spot2check.TYPE == '2XL':
  117.                             tuxscore = tuxscore + 2
  118.                         elif spot2check.TYPE == '2XW':
  119.                             tuxscore = tuxscore + 1
  120.                             multiplier = multiplier * 2
  121.                         elif spot2check.TYPE == '3XL':
  122.                             tuxscore = tuxscore + 3
  123.                         elif spot2check.TYPE == '3XW':
  124.                             tuxscore = tuxscore + 1
  125.                             multiplier = multiplier * 3
  126.                         else:
  127.                             tuxscore = tuxscore + 1
  128.                     
  129.                     tuxscore = tuxscore * multiplier
  130.                     for idx in range(slim - 1, -1, -1):
  131.                         if rlist[idx][0][:3] == 'WC:':
  132.                             rlist.pop(idx)
  133.                         
  134.                     
  135.                     if rval == 1:
  136.                         self.game.tuxscore = self.game.tuxscore + tuxscore
  137.                         return rlist
  138.                     
  139.                 
  140.             
  141.         
  142.         return None
  143.  
  144.     
  145.     def try_col(self, submission):
  146.         board_map = self.board_map
  147.         M = self.M
  148.         N = self.N
  149.         if len(submission) == 0:
  150.             return None
  151.         
  152.         if len(self.board.get_listofheads()) == 0:
  153.             return None
  154.         
  155.         slim = len(submission)
  156.         mlim = (M - len(submission)) + 1
  157.         for n in range(N):
  158.             for m in range(mlim):
  159.                 ok = 1
  160.                 for sidx in range(slim):
  161.                     if submission[sidx][:3] == 'WC:':
  162.                         if board_map[m + sidx][n] == submission[sidx][3:]:
  163.                             dummy = 0
  164.                         else:
  165.                             ok = 0
  166.                     elif board_map[m + sidx][n] == '':
  167.                         dummy = 0
  168.                     else:
  169.                         ok = 0
  170.                 
  171.                 if ok == 1:
  172.                     rlist = []
  173.                     for idx in range(slim):
  174.                         tripple = [
  175.                             submission[idx],
  176.                             m + idx,
  177.                             n]
  178.                         rlist.append(tripple)
  179.                     
  180.                     rval = self.check_neighborhood(rlist)
  181.                     tuxscore = 0
  182.                     multiplier = 1
  183.                     for mm in range(m, m + len(rlist)):
  184.                         spot2check = self.board.get_spotMN(mm, n)
  185.                         if spot2check.TYPE == '2XL':
  186.                             tuxscore = tuxscore + 2
  187.                         elif spot2check.TYPE == '2XW':
  188.                             tuxscore = tuxscore + 1
  189.                             multiplier = multiplier * 2
  190.                         elif spot2check.TYPE == '3XL':
  191.                             tuxscore = tuxscore + 3
  192.                         elif spot2check.TYPE == '3XW':
  193.                             tuxscore = tuxscore + 1
  194.                             multiplier = multiplier * 3
  195.                         else:
  196.                             tuxscore = tuxscore + 1
  197.                     
  198.                     tuxscore = tuxscore * multiplier
  199.                     for idx in range(slim - 1, -1, -1):
  200.                         if rlist[idx][0][:3] == 'WC:':
  201.                             rlist.pop(idx)
  202.                         
  203.                     
  204.                     if rval == 1:
  205.                         self.game.tuxscore = self.game.tuxscore + tuxscore
  206.                         return rlist
  207.                     
  208.                 
  209.             
  210.         
  211.         return None
  212.  
  213.     
  214.     def check_neighborhood(self, rlist):
  215.         board_map = self.board_map
  216.         M = self.M
  217.         N = self.N
  218.         ok = 1
  219.         slen = len(rlist)
  220.         head = (rlist[0][1], rlist[0][2])
  221.         tail = (rlist[slen - 1][1], rlist[slen - 1][2])
  222.         if rlist[0][2] == rlist[1][2]:
  223.             iscol = 1
  224.             col = rlist[0][2]
  225.         else:
  226.             iscol = 0
  227.             row = rlist[0][1]
  228.         if iscol:
  229.             if head[0] == 0:
  230.                 pass
  231.             elif board_map[head[0] - 1][col] != '':
  232.                 ok = -1
  233.                 return ok
  234.             
  235.             if tail[0] == M - 1:
  236.                 pass
  237.             elif board_map[tail[0] + 1][col] != '':
  238.                 ok = -2
  239.                 return ok
  240.             
  241.             if col == 0:
  242.                 pass
  243.             else:
  244.                 for qty in rlist:
  245.                     if qty[0][:3] == 'WC:':
  246.                         pass
  247.                     elif board_map[qty[1]][col - 1] != '':
  248.                         ok = -3
  249.                         return ok
  250.                     
  251.                 
  252.             if col == N - 1:
  253.                 pass
  254.             else:
  255.                 for qty in rlist:
  256.                     if qty[0][:3] == 'WC:':
  257.                         pass
  258.                     elif board_map[qty[1]][col + 1] != '':
  259.                         ok = -4
  260.                         return ok
  261.                     
  262.                 
  263.         elif head[1] == 0:
  264.             pass
  265.         elif board_map[row][head[1] - 1] != '':
  266.             ok = -5
  267.             return ok
  268.         
  269.         if tail[1] == N - 1:
  270.             pass
  271.         elif board_map[row][tail[1] + 1] != '':
  272.             ok = -6
  273.             return ok
  274.         
  275.         if row == 0:
  276.             pass
  277.         else:
  278.             for qty in rlist:
  279.                 if qty[0][:3] == 'WC:':
  280.                     pass
  281.                 elif board_map[row - 1][qty[2]] != '':
  282.                     ok = -7
  283.                     return ok
  284.                 
  285.             
  286.         if head[0] == M - 1:
  287.             pass
  288.         else:
  289.             for qty in rlist:
  290.                 if qty[0][:3] == 'WC:':
  291.                     pass
  292.                 elif board_map[row + 1][qty[2]] != '':
  293.                     ok = -8
  294.                     return ok
  295.                 
  296.             
  297.         return ok
  298.  
  299.  
  300.